#if !MVCUI_CUSTOM_ENABLED
using System;
using System.Collections.Generic;
using ControllerTypeValue = System.Int32;

namespace Life.UI.MVC
{
    /// <summary>
    /// 设计思路：ControllerTypeDefine是一个静态类，用于定义所有的UI页面，每个页面对应一个枚举值。为了便于扩展和发布成Package不考虑Enum
    /// 通过UIPage枚举值，可以在UIManager中查找对应的UIPage，然后显示或隐藏对应的UI页面。
    /// 每个UI设计上可以对应多种继承自UIModel的复合型数据类型，只要UIPage枚举值相同即可。
    /// </summary>
    public static partial class ControllerType
    {
        private static List<Tuple<string, int>> content;

        private static Dictionary<int, string> _nameMapping;

        private static Dictionary<string, int> _valueMapping;


        static ControllerType()
        {
            var fields = typeof(ControllerType).GetFields();
            content = new List<Tuple<string, int>>(fields.Length);
            _nameMapping = new Dictionary<int, string>(fields.Length);
            _valueMapping = new Dictionary<string, int>(fields.Length);
            foreach (var field in fields)
            {
                if (field.FieldType == typeof(int))
                {
                    int value = (int)field.GetValue(null);
                    content.Add(new Tuple<string, int>(field.Name, value));
                    _nameMapping.Add(value, field.Name);
                    _valueMapping.Add(field.Name, value);
                }
            }
            content.Sort((x, y) => x.Item2.CompareTo(y.Item2));
        }

        public static string GetName(int index)
        {
            return content[index].Item1;
        }

        public static ControllerTypeValue GetValue(int index)
        {
            return content[index].Item2;
        }

        public static bool ParseToName(ControllerTypeValue value, out string name)
        {
            return _nameMapping.TryGetValue(value, out name);
        }

        public static bool ParseToValue(string name, out ControllerTypeValue value)
        {
            return _valueMapping.TryGetValue(name, out value);
        }

        public static int Length => content.Count;
    }
}
#endif
